home *** CD-ROM | disk | FTP | other *** search
/ Express Pd: GALORE / Express Pd Galore - The Amiga PD & Shareware CD (1994)(Express Pd)[!][Amiga-CD32-CDTV].iso / productivity / term / termraster.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  8KB  |  453 lines

  1. /*
  2. **    termRaster.c
  3. **
  4. **    Screen character (raster) buffering routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* DeleteRaster():
  13.      *
  14.      *    Free the contents of the character raster.
  15.      */
  16.  
  17. VOID
  18. DeleteRaster()
  19. {
  20.     if(RasterSemaphore)
  21.     {
  22.         FreeVec(RasterSemaphore);
  23.  
  24.         RasterSemaphore = NULL;
  25.     }
  26.  
  27.     if(Raster)
  28.     {
  29.         FreeVec(Raster);
  30.  
  31.         Raster = NULL;
  32.     }
  33.  
  34.     if(RasterAttr)
  35.     {
  36.         FreeVec(RasterAttr);
  37.  
  38.         RasterAttr = NULL;
  39.     }
  40. }
  41.  
  42.     /* CreateRaster():
  43.      *
  44.      *    Create the character raster.
  45.      */
  46.  
  47. BYTE
  48. CreateRaster()
  49. {
  50.         /* Width of the screen * 2 (in characters),
  51.          * extra for double width. The window size
  52.          * may change, the screen size hopefully
  53.          * doesn't.
  54.          */
  55.  
  56.     RasterWidth    = (Window -> WScreen -> Width / TextFontWidth) * 2;
  57.  
  58.         /* Height of the character raster. */
  59.  
  60.     RasterHeight    = Window -> WScreen -> Height / TextFontHeight;
  61.  
  62.         /* Allocate the raster. */
  63.  
  64.     if(Raster = (STRPTR)AllocVec(RasterWidth * RasterHeight,MEMF_ANY | MEMF_CLEAR))
  65.     {
  66.             /* Allocate the raster attributes. */
  67.  
  68.         if(RasterAttr = (STRPTR)AllocVec(RasterHeight,MEMF_ANY | MEMF_CLEAR))
  69.         {
  70.             if(RasterSemaphore = (struct SignalSemaphore *)AllocVec(sizeof(struct SignalSemaphore),MEMF_ANY))
  71.             {
  72.                 InitSemaphore(RasterSemaphore);
  73.  
  74.                 return(TRUE);
  75.             }
  76.         }
  77.     }
  78.  
  79.     DeleteRaster();
  80.  
  81.     return(FALSE);
  82. }
  83.  
  84.     /* RasterEraseScreen(BYTE Mode):
  85.      *
  86.      *    Erase parts of the screen.
  87.      */
  88.  
  89. VOID __regargs
  90. RasterEraseScreen(BYTE Mode)
  91. {
  92.     LONG    First,
  93.         Last;
  94.  
  95.     ObtainSemaphore(RasterSemaphore);
  96.  
  97.     switch(Mode)
  98.     {
  99.         case 1:    First    = 0;
  100.             Last    = CursorY * RasterWidth + CursorX + 1;
  101.  
  102.             memset(RasterAttr,SCALE_NORMAL,CursorY + 1);
  103.  
  104.             break;
  105.  
  106.         case 2:    First    = 0;
  107.             Last    = RasterHeight * RasterWidth - 1;
  108.  
  109.             memset(RasterAttr,SCALE_NORMAL,RasterHeight);
  110.  
  111.             break;
  112.  
  113.         default:First    = CursorY * RasterWidth + CursorX;
  114.             Last    = RasterHeight * RasterWidth - 1;
  115.  
  116.             memset(&RasterAttr[CursorY],SCALE_NORMAL,RasterHeight - CursorY);
  117.  
  118.             break;
  119.     }
  120.  
  121.     if(Last > First)
  122.         memset(&Raster[First],' ',Last - First);
  123.  
  124.     ConFontScaleUpdate();
  125.  
  126.     ReleaseSemaphore(RasterSemaphore);
  127. }
  128.  
  129.     /* RasterEraseLine(BYTE Mode):
  130.      *
  131.      *    Erase parts of the current cursor line.
  132.      */
  133.  
  134. VOID __regargs
  135. RasterEraseLine(BYTE Mode)
  136. {
  137.     LONG    First,
  138.         Last;
  139.  
  140.     ObtainSemaphore(RasterSemaphore);
  141.  
  142.     switch(Mode)
  143.     {
  144.             /* From beginning to current cursor position. */
  145.  
  146.         case 1:    First    = CursorY * RasterWidth;
  147.             Last    = First + CursorX + 1;
  148.  
  149.             break;
  150.  
  151.             /* Entire line. */
  152.  
  153.         case 2:    First    = CursorY * RasterWidth;
  154.             Last    = First + RasterWidth - 1;
  155.  
  156.             break;
  157.  
  158.             /* From current cursor position towards end. */
  159.  
  160.         default:First    = CursorY * RasterWidth + CursorX;
  161.             Last    = (CursorY + 1) * RasterWidth - 1;
  162.  
  163.             break;
  164.     }
  165.  
  166.     if(Last > First)
  167.         memset(&Raster[First],' ',Last - First);
  168.  
  169.     ReleaseSemaphore(RasterSemaphore);
  170. }
  171.  
  172.     /* RasterEraseCharacters(WORD Chars):
  173.      *
  174.      *    Erase a number of characters in the current cursor
  175.      *    line.
  176.      */
  177.  
  178. VOID __regargs
  179. RasterEraseCharacters(WORD Chars)
  180. {
  181.     if(CursorX < RasterWidth - 1)
  182.     {
  183.         LONG     First,
  184.              Diff;
  185.         UBYTE    *To,
  186.             *From;
  187.  
  188.         ObtainSemaphore(RasterSemaphore);
  189.  
  190.         First    = CursorY * RasterWidth + CursorX;
  191.         To    = &Raster[First];
  192.  
  193.         if(CursorX + Chars >= RasterWidth)
  194.         {
  195.             Diff = RasterWidth - 1 - CursorX;
  196.  
  197.             while(Diff-- > 0)
  198.                 *To++ = ' ';
  199.         }
  200.         else
  201.         {
  202.             From    = &Raster[First + Chars];
  203.             Diff    = RasterWidth - (CursorX + 1 + Chars);
  204.  
  205.             while(Diff--)
  206.             {
  207.                 *To++ = *From;
  208.  
  209.                 *From++ = ' ';
  210.             }
  211.         }
  212.  
  213.         ReleaseSemaphore(RasterSemaphore);
  214.     }
  215. }
  216.  
  217.     /* RasterClearLine(WORD Lines):
  218.      *
  219.      *    Clear and remove a number of lines.
  220.      */
  221.  
  222. VOID __regargs
  223. RasterClearLine(WORD Lines,WORD Top)
  224. {
  225.     if(Lines)
  226.     {
  227.         LONG RegionBottom;
  228.  
  229.         ObtainSemaphore(RasterSemaphore);
  230.  
  231.         if(RegionSet)
  232.             RegionBottom = Bottom;
  233.         else
  234.             RegionBottom = LastLine + 1;
  235.  
  236.         if(Top + Lines >= RegionBottom)
  237.             RasterEraseScreen(0);
  238.         else
  239.         {
  240.             LONG     Max;
  241.             UBYTE    *From,
  242.                 *To;
  243.  
  244.             Max    = (RegionBottom - (Top + Lines)) * RasterWidth;
  245.  
  246.             From    = &Raster[(Top + Lines) * RasterWidth];
  247.             To    = &Raster[ Top          * RasterWidth];
  248.  
  249.             while(Max--)
  250.             {
  251.                 *To++ = *From;
  252.  
  253.                 *From++ = ' ';
  254.             }
  255.  
  256.             memset(&RasterAttr[RegionBottom - Lines],SCALE_NORMAL,Lines);
  257.         }
  258.  
  259.         ConFontScaleUpdate();
  260.  
  261.         ReleaseSemaphore(RasterSemaphore);
  262.     }
  263. }
  264.  
  265.     /* RasterInsertLine(WORD Lines):
  266.      *
  267.      *    Insert a number of lines at the current cursor line.
  268.      */
  269.  
  270. VOID __regargs
  271. RasterInsertLine(WORD Lines,WORD Top)
  272. {
  273.     if(Lines)
  274.     {
  275.         LONG RegionBottom;
  276.  
  277.         if(RegionSet)
  278.             RegionBottom = Bottom;
  279.         else
  280.             RegionBottom = LastLine + 1;
  281.  
  282.         if(Top + Lines > RegionBottom)
  283.             RasterEraseScreen(0);
  284.         else
  285.         {
  286.             LONG     From,To,
  287.                  Max;
  288.             UBYTE    *FromPtr,
  289.                 *ToPtr;
  290.  
  291.             ObtainSemaphore(RasterSemaphore);
  292.  
  293.             Max    = (RegionBottom - Lines - Top) * RasterWidth;
  294.  
  295.             From    = (RegionBottom - Lines) * RasterWidth - 1;
  296.             To    =  RegionBottom          * RasterWidth - 1;
  297.  
  298.             FromPtr    = &Raster[From];
  299.             ToPtr    = &Raster[To];
  300.  
  301.             while(Max--)
  302.                 *ToPtr-- = *FromPtr--;
  303.  
  304.             memset(&Raster[Top * RasterWidth],' ',Lines * RasterWidth);
  305.  
  306.             ReleaseSemaphore(RasterSemaphore);
  307.         }
  308.     }
  309. }
  310.  
  311.     /* RasterScrollRegion(WORD Direction,WORD RasterTop,WORD RasterBottom,WORD RasterLines):
  312.      *
  313.      *    Scroll the contents of the character raster up/down.
  314.      */
  315.  
  316. VOID __regargs
  317. RasterScrollRegion(WORD Direction,WORD RasterTop,WORD RasterBottom,WORD RasterLines)
  318. {
  319.     WORD Dir = ABS(Direction);
  320.  
  321.     ObtainSemaphore(RasterSemaphore);
  322.  
  323.     if(Dir >= RasterLines)
  324.     {
  325.             /* All that is needed is to delete the lines. */
  326.  
  327.         memset(&Raster[RasterTop * RasterWidth],' ',RasterLines * RasterWidth);
  328.     }
  329.     else
  330.     {
  331.         LONG     First,
  332.              Last,
  333.              Max,
  334.              i;
  335.         UBYTE    *From,
  336.             *To;
  337.  
  338.         Max = (RasterLines - Dir) * RasterWidth;
  339.  
  340.         if(Direction < 0)
  341.         {
  342.             First    = (RasterTop + RasterLines - Dir) * RasterWidth - 1;
  343.             Last    = (RasterTop + RasterLines    ) * RasterWidth - 1;
  344.  
  345.             From    = &Raster[First];
  346.             To    = &Raster[Last];
  347.  
  348.             while(Max--)
  349.                 *To-- = *From--;
  350.  
  351.             for(i = RasterBottom ; i >= (RasterTop + Dir) ; i--)
  352.                 RasterAttr[i] = RasterAttr[i - Dir];
  353.  
  354.             memset(&Raster[RasterTop * RasterWidth],' ',RasterWidth * Dir);
  355.  
  356.             memset(&RasterAttr[RasterTop],SCALE_NORMAL,Dir);
  357.         }
  358.         else
  359.         {
  360.             First    = RasterTop * RasterWidth + RasterWidth * Dir;
  361.             Last    = RasterTop * RasterWidth;
  362.  
  363.             From    = &Raster[First];
  364.             To    = &Raster[Last];
  365.  
  366.             while(Max--)
  367.                 *To++ = *From++;
  368.  
  369.             memset(&Raster[(RasterBottom - Dir) * RasterWidth],' ',RasterWidth * Dir);
  370.  
  371.             for(i = RasterTop ; i <= (RasterBottom - Dir) ; i++)
  372.                 RasterAttr[i] = RasterAttr[i + Dir];
  373.  
  374.             memset(&RasterAttr[RasterBottom - Dir],SCALE_NORMAL,Dir);
  375.         }
  376.     }
  377.  
  378.     ConFontScaleUpdate();
  379.  
  380.     ReleaseSemaphore(RasterSemaphore);
  381. }
  382.  
  383.     /* RasterShiftChar(WORD Size):
  384.      *
  385.      *    Shift the characters following the current cursor
  386.      *    position Size characters to the right.
  387.      */
  388.  
  389. VOID __regargs
  390. RasterShiftChar(WORD Size)
  391. {
  392.     LONG     i,
  393.          First;
  394.     UBYTE    *From,
  395.         *To;
  396.  
  397.     ObtainSemaphore(RasterSemaphore);
  398.  
  399.     if(CursorX + Size >= RasterWidth - 1)
  400.     {
  401.         i    = RasterWidth - 1 - CursorX;
  402.         To    = &Raster[CursorY * RasterWidth + CursorX];
  403.  
  404.         while(i-- > 0)
  405.             *To++ = ' ';
  406.     }
  407.     else
  408.     {
  409.         First    = (CursorY + 1) * RasterWidth - 1;
  410.         To    = &Raster[First];
  411.  
  412.         From    = &Raster[First - Size];
  413.         i    = RasterWidth - Size;
  414.  
  415.         while(i-- > CursorX)
  416.             *To-- = *From--;
  417.  
  418.         To    = &Raster[CursorY * RasterWidth + CursorX];
  419.  
  420.         while(Size--)
  421.             *To++ = ' ';
  422.     }
  423.  
  424.     ReleaseSemaphore(RasterSemaphore);
  425. }
  426.  
  427.     /* RasterPutString(STRPTR String,WORD Length):
  428.      *
  429.      *    Put a string into the character raster.
  430.      */
  431.  
  432. VOID __regargs
  433. RasterPutString(STRPTR String,WORD Length)
  434. {
  435.     ObtainSemaphore(RasterSemaphore);
  436.  
  437.     if(Length == 1)
  438.     {
  439.         if(CursorX + 1 < RasterWidth)
  440.             Raster[CursorY * RasterWidth + CursorX] = String[0];
  441.     }
  442.     else
  443.     {
  444.         if(CursorX + Length >= RasterWidth)
  445.             Length = RasterWidth - 1 - CursorX;
  446.  
  447.         if(Length > 0)
  448.             memcpy(&Raster[CursorY * RasterWidth + CursorX],String,Length);
  449.     }
  450.  
  451.     ReleaseSemaphore(RasterSemaphore);
  452. }
  453.